package gold.digger;

import gold.utils.InputUtil;
import gold.vo.TreeNode;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1080 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public TreeNode sufficientSubset(TreeNode root, int limit) {
            TreeNode cur = new TreeNode(0);
            cur.left = root;
            dfs(cur, limit, 0);
            return cur.left;
        }

        public boolean dfs(TreeNode root, int limit, int prevSum) {
            if (null == root) return true;
            if (null == root.left && null == root.right) return prevSum + root.val < limit;
            boolean left = dfs(root.left, limit, prevSum + root.val);
            boolean right = dfs(root.right, limit, prevSum + root.val);
            if (left) root.left = null;
            if (right) root.right = null;
            return left && right;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,2,3]");
        System.out.println(solution.sufficientSubset(TreeNode.createTree("[1,2,3,4,-99,-99,7,8,9,-99,-99,12,13,-99,14]"), 1));
    }

    public static void main(String[] args) throws Exception {
        LC1080 an = new LC1080();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
